package com.jww.utils;


import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class MyUtils {
    public static InputStream is = null;
    public static SqlSessionFactory factory = null;

    static {
        try {
            // 1 读取核心配置文件
            is = Resources.getResourceAsStream("sqlConfig.xml");
            // 2 构建工厂对象
            factory = new SqlSessionFactoryBuilder().build(is);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 获取session
    public static SqlSession getSqlSession() {
        return factory.openSession();
    }

    //  释放资源
    public static void close(SqlSession session) {
        session.commit();
        session.close();
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
